package satir.cell;

import java.util.ArrayList;

import satir.card.Deck;
import satir.player.Player;

/**
 * The Class ActionCell that generalize the cell that will draw card.
 * @version 3.0 April 2012
 * @author Creative Family
 */
public class ActionCell extends Cell {

    /** The deck. */
    protected Deck deck;

    /** The next cell. */
    private ActionCell nextCell;

    /** The players that step on this cell. */
    private ArrayList<Player> players;

    /**
     * Instantiates a new draw card cell.
     * @param width
     *            the width
     * @param height
     *            the height
     * @param borderHorizontal
     *            the border horizontal
     * @param borderVertical
     *            the border vertical
     * @param cellLabel
     *            the cell label
     * @param deck
     *            the deck
     */
    public ActionCell(final int width, final int height, final char borderHorizontal,
	    final char borderVertical, final String cellLabel, final Deck deck) {
	super(width, height, borderHorizontal, borderVertical, cellLabel);
	this.players = new ArrayList<Player>();
	this.deck = deck;
    }

    /**
     * Cell action.
     * @param target
     *            the target
     */
    public void cellAction(final Player target) {
	System.out.println("Empty Cell!Nothing to do!!!");
    }

    /**
     * Sets the next cell.
     * @param nextCell
     *            the new next cell
     */
    public void setNextCell(final ActionCell nextCell) {
	this.nextCell = nextCell;
    }

    /**
     * Gets the next cell.
     * @return the next cell
     */
    public ActionCell getNextCell() {
	return nextCell;
    }

    /**
     * Adds the player when player step on this cell.
     * @param player
     *            the player
     */
    public void addPlayer(final Player player) {
	this.players.add(player);
	boolean isSetFig = false;
	for (int i = 1; i < height - 2; i++) {
	    for (int j = 1; j < width - 1; j++) {
		if (cellGround[i][j] == ' ') {
		    cellGround[i][j] = player.getFigure();
		    isSetFig = true;
		    break;
		}
	    }
	    if (isSetFig) {
		break;
	    }
	}
    }

    /**
     * Removes the player when they leave the cell.
     * @param player
     *            the player
     */
    public void removePlayer(final Player player) {
	this.players.remove(player);
	for (int i = 1; i < height - 2; i++) {
	    for (int j = 1; j < width - 1; j++) {
		if (cellGround[i][j] == player.getFigure()) {
		    cellGround[i][j] = ' ';
		}
	    }
	}
    }
}
